package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.base.BaseConfig;
import com.xrz.sxm.aj.entity_tab.DBHelps;
import com.xrz.sxm.aj.entity_tab.Sjsj;
import com.xrz.sxm.aj.https.Http;
import com.xrz.sxm.aj.https.HttpConst;
import com.xrz.sxm.aj.https.HttpListener;
import com.xrz.sxm.aj.utils.ScreenUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import tf.common.jsonutils.JSONUtils;

public class Act_Welcome extends BaseAct implements AnimationListener {
	private Animation m_Animation;
	@ViewInject(R.id.img_welcome)
	private ImageView m_ImageView;

	private DBHelps dbHelps;

	@ViewInject(R.id.pb)
	private ProgressBar m_Bar;

	@ViewInject(R.id.tv_msg)
	private TextView tv_msg;
	@ViewInject(R.id.pb_s)
	private ProgressBar m_Bar_s;
	@ViewInject(R.id.view_progress)
	private View view_progress;

//	@ViewInject(R.id.row)
//	private View row;

	@Override
	protected int setView() {
		return R.layout.layout_welcome;
	}

	@Override
	protected void init() {
		dbHelps = new DBHelps(ctx);
		ScreenUtil.getInstant().init(this);
		createFolder();
		m_Animation = new AlphaAnimation(0.1f, 1.0f);
		m_Animation.setDuration(2000);
		m_ImageView.setAnimation(m_Animation);
		m_ImageView.startAnimation(m_Animation);
		m_Animation.setAnimationListener(this);

	}

	/**
	 * 功能：创建文件夹
	 */
	private void createFolder() {
		File file = null;
		file = new File(BaseConfig.BASE_IMG_CACHE);
		if (file.exists() == false) {
			file.mkdirs();
		}
	}

	@Override
	protected String setTopTitle() {
		return null;
	}

	@Override
	protected boolean topHide() {
		return true;
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// 首先拷贝本地数据库到手机
//		new Thread() {
//			public void run() {
//				DBManager dbManager = new DBManager(ctx);
//				dbManager.copy();
//				handler.sendEmptyMessage(10);
//			}
//
//			;
//		}.start();
		handler.sendEmptyMessage(10);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}

	@Override
	public void onAnimationStart(Animation animation) {
	}

	private void update(final List<Sjsj> list) {
		final List<Sjsj> updateList = new ArrayList<>();
		for (Sjsj sjsj : list) {
			if (dbHelps.isUpdate(sjsj)) {
				try {
					Log.e(TAG, sjsj.bmc + "  需要更新");
					updateList.add(sjsj);
				} catch (Exception e) {
				}
			} else {
				Log.e(TAG, sjsj.bmc + "  不用跟新");
			}
		}

		if (updateList.size() == 0) {
			handler.sendEmptyMessage(1);
		} else {
			final List<Tws> lists = new ArrayList<>();
			m_Bar.setMax(updateList.size());
			new Thread() {
				public void run() {
					for (Sjsj sjsj : updateList) {
						try {
							Tws tws = new Tws();
							tws.sjsj = sjsj;
							tws.list = update(sjsj, sjsj.bmc);
							lists.add(tws);
						} catch (Exception e) {
						}
					}
					handler.sendEmptyMessage(13);
					for (Tws tws : lists) {
						try {
							int i = 0;
							String tablename = tws.sjsj.bmc;
							tablename = tablename.replaceFirst(tablename.substring(0,
									1), tablename.substring(0, 1).toUpperCase());
							Class class1 = Class.forName("com.xrz.sxm.aj.entity_tab." + tablename);
							dbHelps.del(class1);
							dbHelps.save(tws.list, new DBHelps.SaveListener() {
								@Override
								public void save(int progress, int totle) {
									Message msg = Message.obtain();
									msg.arg1 = progress;
									msg.arg2 = totle;
									msg.what = 11;
									handler.sendMessage(msg);
								}
							});
							dbHelps.updateSjsj(tws.sjsj);
							i++;
							Message msg = Message.obtain();
							msg.arg1 = i;
							msg.what = 2;
							handler.sendMessage(msg);
						}catch (Exception e){

						}
					}
					handler.sendEmptyMessage(1);
				}

				;
			}.start();
		}


	}

	private class Tws {
		public Sjsj sjsj;
		public List list;
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1:
					startActivity(new Intent(ctx, Act_Main.class));
					finish();
					break;
				case 2:
					m_Bar.setProgress(msg.arg1);
					break;
				case 10:
					Http.getInstant().sendRequest(getApplicationContext(), false, HttpConst.URL_UPDATE, null, new HttpListener() {
						@Override
						public void onHttpSuccess(String result) {
							try {
								List<Sjsj> list = new ArrayList<Sjsj>();
								JSONArray array = new JSONArray(result);
								for (int i = 0; i < array.length(); i++) {
									JSONObject object = array.getJSONObject(i);
									Sjsj sj = new Sjsj();
									sj.setBmc(object.getString("bmc"));
									sj.setSjbz(object.getInt("sjbz"));
									list.add(sj);
								}
								update(list);
							} catch (Exception e) {

							}
						}

						@Override
						public void onHttpError(String error) {
							startActivity(new Intent(ctx, Act_Main.class));
							finish();
						}
					});
					break;
				case 11:
					m_Bar_s.setMax(msg.arg2);
					m_Bar_s.setProgress(msg.arg1);
					tv_msg.setText(msg.arg1 + "/" + msg.arg2);
					break;
				case 13:
					view_progress.setVisibility(View.VISIBLE);
					break;
			}
		}

		;
	};

	private List update(Sjsj sjsj, final String tablename) throws Exception {
		final String tablename1 = tablename.replaceFirst(tablename.substring(0,
				1), tablename.substring(0, 1).toUpperCase());
		String result = Http.getInstant().sendRequestSync(HttpConst.BASEURL+tablename, null);
		Class class1 = Class.forName("com.xrz.sxm.aj.entity_tab." + tablename1);
		List list = JSONUtils.getJSONResult(result, class1);
		return list;
	}

}
